package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/13 9:46
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询当前库存（根据商品类别、商品编码或名称搜索）
     * @return
     */
    @PostMapping("/listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){

        return goodsService.listInventory(page,rows,codeOrName,goodsTypeId);

    }

    // http://localhost:8080/goods/list

    /**
     * 根据分类、名称查询所有商品信息
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        return goodsService.list(page,rows,goodsName,goodsTypeId);
    }

    // http://localhost:8080/goods/save?goodsId=37
    // 保存或修改
    @PostMapping("save")
    public ServiceVO save(Goods goods,Integer goodsId){
        try {
            if (!StringUtils.isEmpty(goodsId)){
                goodsService.updateGoods(goods);
            }else {
                goodsService.saveGoods(goods);
            }
        } catch (Exception e) {
            return new ServiceVO(400, "保存失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

     // http://localhost:8080/goods/delete
    @PostMapping("/delete")
    public ServiceVO deleteGoods(Integer goodsId){

        return goodsService.deleteGoods(goodsId);

    }

    // http://localhost:8080/goods/getCode
    @PostMapping("/getCode")
    public ServiceVO getCode(){
        return goodsService.getCode();
    }

    // http://localhost:8080/goods/getNoInventoryQuantity

    /**
     * 无库存商品列表展示
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        return goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    // http://localhost:8080/goods/getHasInventoryQuantity

    /**
     * 有库存商品列表展示
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        return goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
    }

    // http://localhost:8080/goods/saveStock?goodsId=25

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @PostMapping("/saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        try {
            goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        } catch (Exception e) {
            return new ServiceVO(400, "保存失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    // http://localhost:8080/goods/deleteStock

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        try {
            goodsService.deleteStock(goodsId);
        } catch (Exception e) {
            return new ServiceVO(400, "删除失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    // http://localhost:8080/goods/listAlarm
    @PostMapping("/listAlarm")
    public Map<String,Object> listAlarm(){
        return goodsService.listAlarm();
    }



}
